#include "Client.h"
#include "ClientThread.h"

union _fct_ptr {
  void* ptr;
  int (* fct)(int, void*);
};

ClientThread::ClientThread(Client* cli, const char* _name){
	client = cli;
	exit = false;
	running = false;
	name = _name;

	_fct_ptr ptr;
	ptr.fct = &ClientThread::entry;
	instance = geCreateThread(_name, ptr.ptr, 0);
}

ClientThread::~ClientThread(){
}

void ClientThread::Start(){
	geThreadStart(instance, sizeof(void*), this);
}

void ClientThread::Pause(){
	running = false;
}

void ClientThread::Stop(){
	exit = true;
	while(running){
		geSleep(1);
	}
}

void ClientThread::run(){
}

bool ClientThread::isRunning(){
	return running;
}

int ClientThread::entry(int args, void* argp){
	ClientThread* handle = (ClientThread*)argp;
	handle->running = true;
	handle->exit = false;
	while(!handle->exit){
		while(!handle->running){
			geSleep(1);
		}
		handle->run();
	}
	handle->running = false;
	geThreadExit(0);
	return 0;
}
